package com.pa.chong.pachong.tool;

import com.pa.chong.pachong.tool.dao.PhotoTypeDao;
import com.pa.chong.pachong.tool.utils.MyFuture;
import com.pa.chong.pachong.tool.dowork.PaMMSingle;
import com.pa.chong.pachong.tool.dowork.PaTu;
import com.pa.chong.pachong.tool.utils.DownloadImage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by pangkunkun on 2017/9/3.
 * 在项目启动后会调用这里的方法
 */
@Component
public class MyCommandLineRunner implements CommandLineRunner {

    ExecutorService fixedThreadPool = Executors.newFixedThreadPool(2);

    @Value("${myconfig.imgType}")
    private String imgType;
    @Value("${myconfig.photoPath}")
    private String path;

    @Autowired
    private DownloadImage downloadImage;

    @Autowired
    private PhotoTypeDao photoTypeDao;

    @Override
    public void run(String... var1) throws Exception {
//        pamm();
//        patu();
        pammSingle();
    }



    public void pammSingle(){

        String url = "https://www.mzitu.com/xinggan/page/";

        for (int i = 1;i<=149;i++){
            String realUrl = url + i;
            MyFuture myFuture = new MyFuture(new PaMMSingle(path,realUrl,imgType,downloadImage,photoTypeDao));
            fixedThreadPool.submit(myFuture);
        }

        fixedThreadPool.shutdown();
    }


    public void patu() {


        String url = "http://www.mzitu.com/all/";
        new PaTu(imgType, downloadImage, photoTypeDao, url).start();
    }
}
